library(tidyverse) # for data cleaning and plotting
library(gardenR) # for Lisa's garden data
library(lubridate) # for date manipulation
library(openintro) # for the abbr2state() function
library(palmerpenguins)# for Palmer penguin data
library(maps) # for map data
library(ggmap) # for mapping points on maps
library(gplots) # for col2hex() function
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
library(leaflet) # for highly customizable mapping
library(ggthemes) # for more themes (including theme_map())
library(plotly) # for the ggplotly() - basic interactivity
library(gganimate) # for adding animation layers to ggplots
library(transformr) # for "tweening" (gganimate)
library(gifski) # need the library for creating gifs but don't need to load each time
library(shiny) # for creating interactive apps
library(ggimage)
theme_set(theme_minimal())
# SNCF Train data
small_trains <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-02-26/small_trains.csv")
# Lisa's garden data
data("garden_harvest")
# Lisa's Mallorca cycling data
mallorca_bike_day7 <- read_csv("https://www.dropbox.com/s/zc6jan4ltmjtvy0/mallorca_bike_day7.csv?dl=1") %>%
select(1:4, speed)
# Heather Lendway's Ironman 70.3 Pan Am championships Panama data
panama_swim <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_swim_20160131.csv")
panama_bike <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_bike_20160131.csv")
panama_run <- read_csv("https://raw.githubusercontent.com/llendway/gps-data/master/data/panama_run_20160131.csv")
#COVID-19 data from the New York Times
covid19 <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
Go here or to previous homework to remind yourself how to get set up.
Once your repository is created, you should always open your project rather than just opening an .Rmd file. You can do that by either clicking on the .Rproj file in your repository folder on your computer. Or, by going to the upper right hand corner in R Studio and clicking the arrow next to where it says Project: (None). You should see your project come up in that list if you’ve used it recently. You could also go to File –> Open Project and navigate to your .Rproj file.
Put your name at the top of the document.
For ALL graphs, you should include appropriate labels.
Feel free to change the default theme, which I currently have set to theme_minimal().
Use good coding practice. Read the short sections on good code with pipes and ggplot2. This is part of your grade!
NEW!! With animated graphs, add eval=FALSE to the code chunk that creates the animation and saves it using anim_save(). Add another code chunk to reread the gif back into the file. See the tutorial for help.
When you are finished with ALL the exercises, uncomment the options at the top so your document looks nicer. Don’t do it before then, or else you might miss some important warnings and messages.
ggplotly() function.# total harvest in pounds for each tomato variety
tomato_varieity_graph <- garden_harvest %>%
filter(vegetable=="tomatoes") %>% # subseting tomatoes
group_by(vegetable, variety) %>%
summarise(first_harvest_date=min(date), total_weight_lbs = sum(weight)*0.00220462) %>% # get the first harvest date for each variety, and get the sum of weight for each variety in pounds
arrange(first_harvest_date) %>% # sort by first_harvest_date => from earliest to latest
ggplot(aes(y=reorder(variety, first_harvest_date),x=total_weight_lbs))+ # plot
geom_col()+
labs(y="variety (from smallest to largest by first harvest date)") # label
ggplotly(tomato_varieity_graph)
# Change of Total Weight of Harvested Overtime
weight_harvested_graph <- garden_harvest %>%
mutate(month = month(date, label = TRUE)) %>%
group_by(month) %>%
summarize(total_weight = sum(weight* 0.00220462)) %>%
ggplot(aes(x = month, y = total_weight, fill=total_weight))+
labs(x = "Month", y ="Weight of Vegetables Harvested (lbs)", title = "Change of Total Weight of Harvested Overtime")+
theme_igray()+
geom_bar(stat = "identity")
ggplotly(weight_harvested_graph)
small_trains dataset that contains data from the SNCF (National Society of French Railways). These are Tidy Tuesday data! Read more about it here.small_trains %>%
mutate(date=date(paste(year,month,"01",sep = "-"))) %>%
group_by(date) %>%
summarise(total_trips_by_month = sum(total_num_trips)) %>%
ggplot(aes(x=date, y= total_trips_by_month))+
geom_line(color="pink", size=1)+
labs(y = "Total number of trips by time")+
transition_reveal(date)
geom_area() examples here). You will look at cumulative harvest of tomato varieties over time. You should do the following:garden_harvest data, filter the data to the tomatoes and find the daily harvest in pounds for each variety.fct_reorder()) from most to least harvested (most on the bottom).I have started the code for you below. The complete() function creates a row for all unique date/variety combinations. If a variety is not harvested on one of the harvest dates in the dataset, it is filled with a value of 0.
garden_harvest %>%
filter(vegetable == "tomatoes") %>%
group_by(date, variety) %>%
summarize(daily_harvest_lb = sum(weight)*0.00220462) %>%
ungroup() %>%
complete(variety, date, fill = list(daily_harvest_lb = 0)) %>%
mutate(cum_harvest_lb = cumsum(daily_harvest_lb)) %>%
ggplot(aes(x=date, y= cum_harvest_lb, fill=fct_reorder(variety, cum_harvest_lb)))+
geom_area()+
labs(y=" cumulative harvest in pounds")+
scale_fill_discrete(name = "variety")+
transition_reveal(date)
mallorca_bike_day7 bike ride using animation! Requirements:ggmap.ggimage package and geom_image to add a bike image instead of a red point. You can use this image. See here for an example.bike_image = "https://raw.githubusercontent.com/llendway/animation_and_interactivity/master/bike.png"
mallorca_bike_day7_with_image <- mallorca_bike_day7 %>%
mutate(image = bike_image)
mallorca_map <- get_stamenmap(
bbox = c(left = 2.38, bottom = 39.55, right = 2.62, top = 39.7),
maptype = "terrain",
zoom = 11
)
ggmap(mallorca_map) +
geom_path(data = mallorca_bike_day7_with_image,
aes(x=lon, y=lat, color=ele),size=2) +
geom_image(data = mallorca_bike_day7_with_image,
aes(image = image),
size = 0.1)+
labs(subtitle = "Time: {frame_along}")+
annotate(geom = "point", x = 2.586255, y = 39.66033, color = "red",size=4)+
scale_color_gradient2( # added the color
midpoint = 275,
low = "black",
mid = "purple",
high = "orange")+
transition_reveal(time)
I feel like this map is better as it can directly visualize the location at each specific time.
panama_swim, panama_bike, and panama_run. Create a similar map to the one you created with my cycling data. You will need to make some small changes: 1. combine the files (HINT: bind_rows(), 2. make the leading dot a different color depending on the event (for an extra challenge, make it a different image using `geom_image()!), 3. CHALLENGE (optional): color by speed, which you will need to compute on your own from the data. You can read Heather’s race report here. She is also in the Macalester Athletics Hall of Fame and still has records at the pool.panama <- bind_rows(panama_swim,panama_bike,panama_run)
panama_swim
panama_bike
panama_run
panama_map <- get_stamenmap(
bbox = c(left = -79.5745, bottom = 8.8924, right = -79.4948, top = 8.9969),
maptype = "terrain",
zoom = 13
)
ggmap(panama_map)+
geom_path(data = panama,
aes(x=lon, y=lat, group=event),size=1) +
labs(subtitle = "Time: {frame_along}")+
annotate(geom = "point", x = -79.54469, y = 8.928600, color = "red",size=4)+
annotate(geom = "point", x = -79.51783, y = 8.975294, color = "green",size=4)+
annotate(geom = "point", x = -79.55428, y = 8.939356, color = "blue",size=4)+
scale_color_gradient2( # added the color
midpoint = 45,
low = "black",
mid = "purple",
high = "orange")+
transition_reveal(time)